"use strict";

module.exports = (app) => {
  const { STRING, INTEGER, DECIMAL, BOOLEAN, DATE } = app.Sequelize;

  const ProductSpec = app.model.define("product_spec", {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    product_id: {
      type: INTEGER,
      allowNull: false,
      comment: "商品ID",
    },
    spec_name: {
      type: STRING(50),
      allowNull: false,
      comment: "规格名称(如:颜色,尺寸)",
    },
    spec_value: {
      type: STRING(100),
      allowNull: false,
      comment: "规格值(如:红色,XL)",
    },
    price: {
      type: DECIMAL(10, 2),
      comment: "该规格价格(为空则使用商品默认价格)",
    },
    cost_price: {
      type: DECIMAL(10, 2),
      comment: "该规格成本价",
    },
    stock: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: "该规格库存",
    },
    image: {
      type: STRING(255),
      comment: "该规格图片",
    },
    sku: {
      type: STRING(50),
      comment: "SKU编码",
    },
    sort_order: {
      type: INTEGER,
      allowNull: false,
      defaultValue: 0,
      comment: "排序",
    },
    created_at: DATE,
    updated_at: DATE,
  });

  // 关联关系
  ProductSpec.associate = function () {
    // 规格与商品：多对一关系
    app.model.ProductSpec.belongsTo(app.model.Product, {
      foreignKey: "product_id",
    });
  };

  return ProductSpec;
};
